<script lang="ts" setup>
import {Question, User} from "../../types/mytypes";
import {onMounted, ref} from "vue";
import {TagRequest, UserRequest} from "../../request/MyRequset";

const props = withDefaults(defineProps<{
  question: Question,
  width?: number
}>(), {
  width: 20
})
let tagTextList = ref<string[]>([])
let author = ref<User>({} as User)

onMounted(async () => {
  //查询该question的tag
  let tagRes = await TagRequest.getByQuestion(props.question.id as number)
  if (tagRes.suc) {
    for (let i in tagRes.data) {
      tagTextList.value.push(tagRes.data[i].name)
    }
  }
  //查询该question的发布者
  let userRes = await UserRequest.getById(props.question.u_id as number)
  if(userRes.suc){
    author.value = userRes.data
  }
})
</script>

<template>
  <div class="question_item_box" :style="{'width':width+'em'}">
    <div class="left">
      <div class="left_text">
        <p>{{ question.content[0] }}</p>
      </div>
    </div>
    <div class="center">
      <div class="content_text">
        <el-text style="padding: 0;
      margin: 0;
      font-size: 0.8em;max-width: 16em" truncated>{{ question.content }}
        </el-text>
      </div>
      <div class="center_tags">
        <el-tag v-for="item in tagTextList">{{ item }}</el-tag>
      </div>
    </div>
    <div class="right">
      <el-text style="font-size: 0.5em">来自</el-text>
      <el-text style="font-size: 0.5em;font-weight: bold" type="primary">{{ author.name }}</el-text>
    </div>
  </div>
</template>

<style scoped lang="scss">
.question_item_box {
  padding: 2px;
  border: 1px solid #ccc;
  border-radius: 4px;
  height: auto;
  min-height: 4em;
  margin: 0.1em;
  display: flex;
  flex-direction: row;
  align-items: center;
  justify-content: space-between;
  box-sizing: border-box;
  overflow: hidden;

  .left {
    width: 5em;
    height: 100%;
    display: flex;
    flex-direction: row;
    align-items: center;
    justify-content: center;
    user-select: none;

    .left_text {
      display: flex;
      flex-direction: row;
      align-items: center;
      justify-content: center;
      width: 3em;
      height: 3em;
      background-color: #409eff;
      border-radius: 2px;

      p {
        padding: 0;
        margin: 0;
        font-weight: bold;
        font-size: 1em;
        color: white;
      }
    }
  }

  .center {
    display: flex;
    flex-direction: column;
    width: 100%;

    .center_tags {
      display: flex;
      flex-direction: row;
    }

    .content_text {
      display: flex;
      flex-direction: row;
      justify-content: start;

    }
  }

  .right{
    max-width: 5em;
    min-width: 3em;
    display: flex;
    flex-direction: column;
  }
}
</style>